﻿module Sustainalytics.EsgRatings {

    export class EsgThemeOverallPerformanceController {

        // overall performance
        public overall: EsgThemeOverallPerformanceSummaryDto;
        public overall_overall_esgPositionName: string;

        private userId: string;
        private esgProfileId: string;

        constructor(
            private $scope: IEsgThemeScope,
            private $timeout: ng.ITimeoutService,
            private $q: ng.IQService,
            private EsgThemeOverallPerformanceSummaryService: IEsgThemeOverallPerformanceSummaryService,
            private $rootScope) {

            var ctx = this;

            if (ctx.$rootScope['userInfo'] == null) {
                ctx.$rootScope['userInfo'] = angular.fromJson(localStorage.getItem('sust_userInfo'));
            }
            ctx.userId = ctx.$rootScope['userInfo'].userId;
            ctx.esgProfileId = ctx.$rootScope['userInfo'].esgProfileId;

            ctx.init();
        }

        private init(): ng.IPromise<any> {

            var ctx = this;

            var promise = ctx.EsgThemeOverallPerformanceSummaryService.get(ctx.$scope.currentCompany.id, ctx.esgProfileId, ctx.$scope.currentTheme);

            promise.then(
                // success
                function (data) {

                    ctx.$timeout(function () {
                        ctx.$scope.$apply(function () {
                            ctx.overall = data;
                            ctx.overall_overall_esgPositionName = data.overall.esgPosition.name;
                        });
                    });
                },() => {

                    ga('create', 'UA-4379897-6', {
                        'userId': ctx.userId, 'cookieDomain': 'none', 'name': 'globalAccess'
                    });

                    ga('globalAccess.send', 'exception', { 'exDescription': 'Esg Ratings: Overall Performance Controller -> Error initializing' });
                });

            return promise;
        }

        public getEsgPosition(): any {
            return EsgPositionEnum;
        }

    }


    export var esgThemeOverallPerformanceControllerName = 'EsgThemeOverallPerformanceController';

    angular.module(esgRatingsModuleName).controller(esgThemeOverallPerformanceControllerName,
        ['$scope', '$timeout', '$q', 'EsgThemeOverallPerformanceSummaryService', '$rootScope', EsgThemeOverallPerformanceController]);
} 